package util;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

/**
 * @author Administrator
 * Servlet操作工具类
 */
public class ServletUtil {
	
	/**
	 * @param req
	 * @param res
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @throws JSONException
	 * 读取请求Json返回Json对象
	 */
	public static JSONObject red_req_json(HttpServletRequest req, HttpServletResponse res) 
			throws ServletException, IOException, JSONException {
		req.setCharacterEncoding("utf-8");
		BufferedReader br = null;
		br = req.getReader();
		/*char[] cs = new char[64];
		int len = -1;*/
		StringBuffer sb = new StringBuffer();
		String temp = null;
		while((temp = br.readLine()) != null) {
			sb.append(temp);
		}
		/*while((len = br.read(cs)) != -1){
			temp = new String(cs,0,len);
			sb.append(temp);
		}*/
		if(br != null){
			br.close();
		}
		temp = null;
		String result = sb.toString();
		//！！！！！！！！！！！！！！！！ajax编码一次，java解码两次，解决ajax post请求中文乱码！！
		result = URLDecoder.decode(URLDecoder.decode(result,"UTF-8"),"UTF-8");
		/*result = URLDecoder.decode(URLDecoder.decode(result,"UTF-8"),"UTF-8");
		result = new String(result.getBytes("ISO-8859-1"),"UTF-8");
		result = URLEncoder.encode(sb.toString(), "ISO-8859-1");
		//再进行utf-8编码 一次得到页面上输入的文本内容
	    result = URLDecoder.decode(result, "UTF-8");*/
		System.out.println("请求Json为："+result);
		JSONObject json = new JSONObject(result);
		return json;
	}
	
	/**
	 * @param req
	 * @param res
	 * @param json
	 * @throws ServletException
	 * @throws IOException
	 * @throws JSONException
	 * 响应Json对象 字符串写出
	 */
	public static void res_send_json(HttpServletRequest req, HttpServletResponse res, JSONObject json) 
			throws ServletException, IOException, JSONException{
		PrintWriter pw = null;
		pw = res.getWriter();
		System.out.println("响应结果为：" + json.toString());
		pw.write(json.toString());
		if(pw != null){
			pw.close();
		}
		System.out.println("写出json字符串完毕");
	}
	
	
	/**
	 * @param req
	 * @return
	 * 返回base64位验证码图片字符串，更新session_img
	 */
	public static String update_img(HttpServletRequest req) {
			//生成随机的验证码和图片                                                                                  
			Object[] objs = ImageUtil.createImage();
			//将验证码存入session
			HttpSession session = req.getSession();
			session.setAttribute("imgCode", objs[0]);
			//将图片输出给浏览器
			BufferedImage img = (BufferedImage) objs[1];
			//该输出流的目标就是浏览器
			/*OutputStream os = null;
			os = res.getOutputStream();*/
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			try {
				ImageIO.write(img, "png", bos);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			byte[] bytes = bos.toByteArray();
			String s = new String(Base64.encode(bytes));
			System.out.println(s);
			try {
				bos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return s;
		}
}
